/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import myJPA.Model;
import user.Customer;

/**
 *
 * @author nacerkhalil
 */
public class DeleteCustomer extends javax.swing.JPanel {

    /**
     * Creates new form DeleteCustomer
     */
    public DeleteCustomer() {
        initComponents();
        this.CustomerNotFoundLabel.setVisible(false);
        this.searchInfoPanel.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        searchInfoPanel = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        removeButton = new javax.swing.JButton();
        searchButton = new javax.swing.JButton();
        CustomerNotFoundLabel = new javax.swing.JLabel();

        jLabel1.setText("Enter customer id");

        jLabel2.setText("First name:");

        jLabel3.setText("Last name:");

        jTextField2.setEnabled(false);

        jTextField3.setEnabled(false);

        removeButton.setText("Delete customer");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout searchInfoPanelLayout = new org.jdesktop.layout.GroupLayout(searchInfoPanel);
        searchInfoPanel.setLayout(searchInfoPanelLayout);
        searchInfoPanelLayout.setHorizontalGroup(
            searchInfoPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(searchInfoPanelLayout.createSequentialGroup()
                .add(29, 29, 29)
                .add(searchInfoPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(removeButton)
                    .add(searchInfoPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(searchInfoPanelLayout.createSequentialGroup()
                            .add(jLabel3)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 173, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(searchInfoPanelLayout.createSequentialGroup()
                            .add(jLabel2)
                            .add(18, 18, 18)
                            .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 173, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        searchInfoPanelLayout.setVerticalGroup(
            searchInfoPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(searchInfoPanelLayout.createSequentialGroup()
                .add(8, 8, 8)
                .add(searchInfoPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(searchInfoPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 50, Short.MAX_VALUE)
                .add(removeButton)
                .addContainerGap())
        );

        searchButton.setText("Search");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        CustomerNotFoundLabel.setForeground(new java.awt.Color(255, 0, 0));
        CustomerNotFoundLabel.setText("There is no customer associate with this ID");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(102, 102, 102)
                        .add(searchInfoPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(135, 135, 135)
                        .add(CustomerNotFoundLabel))
                    .add(layout.createSequentialGroup()
                        .add(92, 92, 92)
                        .add(jLabel1)
                        .add(18, 18, 18)
                        .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 173, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(searchButton)))
                .addContainerGap(278, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(45, 45, 45)
                .add(CustomerNotFoundLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(searchButton))
                .add(18, 18, 18)
                .add(searchInfoPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(193, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        if(customer != null)
        {
            customer.remove();
            searchInfoPanel.setVisible(false);
        }
    }//GEN-LAST:event_removeButtonActionPerformed

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        try
        {
            CustomerNotFoundLabel.setVisible(false);
            jTextField2.setText("");
            jTextField3.setText("");
            int id = 0;
            id = Integer.parseInt(jTextField1.getText());
            
            customer = Model.getEntityManager().find(Customer.class, id);
            jTextField2.setText(customer.getFirstName());
            jTextField3.setText(customer.getLastName());
            this.searchInfoPanel.setVisible(true);
        }
        catch(Exception e)
        {
            CustomerNotFoundLabel.setVisible(true);
            this.searchInfoPanel.setVisible(false);
        }
    }//GEN-LAST:event_searchButtonActionPerformed
    
    //Service objects
    private Customer customer;
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel CustomerNotFoundLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JButton removeButton;
    private javax.swing.JButton searchButton;
    private javax.swing.JPanel searchInfoPanel;
    // End of variables declaration//GEN-END:variables
}
